<?php
/**
 * 查看签到系统配置
 */

$host = '113.44.136.17';
$port = '3306';
$dbname = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$dbname};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "=== 签到系统配置 ===\n\n";

    // 查询配置
    $stmt = $pdo->query("SELECT * FROM checkin_setting WHERE store_id = 0");
    $config = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($config) {
        echo "【基础配置】\n";
        echo "单位时长: {$config['unit_minutes']} 分钟\n";
        echo "取整方式: {$config['rounding']}\n";
        echo "超时时长: {$config['timeout_minutes']} 分钟\n";
        echo "积分有效期: {$config['points_expire_days']} 天\n";
        echo "\n";

        echo "【时间段配置】\n";
        if (!empty($config['time_slots'])) {
            $timeSlots = json_decode($config['time_slots'], true);
            if (is_array($timeSlots) && count($timeSlots) > 0) {
                foreach ($timeSlots as $index => $slot) {
                    echo "时间段" . ($index + 1) . ": {$slot['start']} - {$slot['end']}, 每小时 {$slot['points_per_hour']} 积分\n";
                }
            } else {
                echo "无时间段配置\n";
            }
        } else {
            echo "无时间段配置\n";
        }
        echo "\n";

        echo "【积分计算规则说明】\n";
        $timeSlots = json_decode($config['time_slots'], true);
        $hasTimeSlots = is_array($timeSlots) && count($timeSlots) > 0;

        if ($hasTimeSlots) {
            echo "✅ 使用时间段规则计算积分\n";
            echo "   - 根据签到开始时间所在的时间段，使用对应的每小时积分\n";
            echo "   - 积分 = 签到时长（小时）× 每小时积分\n";
            echo "   - 取整方式: {$config['rounding']}\n";
        } else {
            echo "✅ 使用基础规则计算积分\n";
            echo "   - 每满 {$config['unit_minutes']} 分钟记 1 分\n";
            echo "   - 取整方式: {$config['rounding']}\n";
        }
        echo "\n";

        echo "【计算示例】\n";
        if ($hasTimeSlots) {
            $slot = $timeSlots[0];
            echo "假设在 {$slot['start']} - {$slot['end']} 时间段内签到：\n";
            echo "  - 签到 30 分钟 = 0.5 小时 × {$slot['points_per_hour']} = " . (0.5 * $slot['points_per_hour']);
            if ($config['rounding'] == 'floor') {
                echo " → 向下取整 = " . floor(0.5 * $slot['points_per_hour']) . " 分\n";
            } elseif ($config['rounding'] == 'ceil') {
                echo " → 向上取整 = " . ceil(0.5 * $slot['points_per_hour']) . " 分\n";
            } else {
                echo " → 四舍五入 = " . round(0.5 * $slot['points_per_hour']) . " 分\n";
            }

            echo "  - 签到 60 分钟 = 1 小时 × {$slot['points_per_hour']} = " . (1 * $slot['points_per_hour']) . " 分\n";
            echo "  - 签到 90 分钟 = 1.5 小时 × {$slot['points_per_hour']} = " . (1.5 * $slot['points_per_hour']);
            if ($config['rounding'] == 'floor') {
                echo " → 向下取整 = " . floor(1.5 * $slot['points_per_hour']) . " 分\n";
            } elseif ($config['rounding'] == 'ceil') {
                echo " → 向上取整 = " . ceil(1.5 * $slot['points_per_hour']) . " 分\n";
            } else {
                echo " → 四舍五入 = " . round(1.5 * $slot['points_per_hour']) . " 分\n";
            }
        } else {
            echo "假设单位时长为 {$config['unit_minutes']} 分钟：\n";
            echo "  - 签到 5 分钟 = 5 / {$config['unit_minutes']} = " . (5 / $config['unit_minutes']);
            if ($config['rounding'] == 'floor') {
                echo " → 向下取整 = " . floor(5 / $config['unit_minutes']) . " 分\n";
            } elseif ($config['rounding'] == 'ceil') {
                echo " → 向上取整 = " . ceil(5 / $config['unit_minutes']) . " 分\n";
            } else {
                echo " → 四舍五入 = " . round(5 / $config['unit_minutes']) . " 分\n";
            }

            echo "  - 签到 10 分钟 = 10 / {$config['unit_minutes']} = " . (10 / $config['unit_minutes']) . " 分\n";
            echo "  - 签到 25 分钟 = 25 / {$config['unit_minutes']} = " . (25 / $config['unit_minutes']);
            if ($config['rounding'] == 'floor') {
                echo " → 向下取整 = " . floor(25 / $config['unit_minutes']) . " 分\n";
            } elseif ($config['rounding'] == 'ceil') {
                echo " → 向上取整 = " . ceil(25 / $config['unit_minutes']) . " 分\n";
            } else {
                echo " → 四舍五入 = " . round(25 / $config['unit_minutes']) . " 分\n";
            }
        }

    } else {
        echo "❌ 未找到系统配置\n";
    }

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

